/**
 * Class for flash movies on the web page.
 * Not tested yet. Will be removed since it's a temporary replacement
 * for core.flash classes which are under construction now.
 *
 * @author Andrey Anisimov <hercules@inbox.ru>
 * @see core.flash package.
 */

import core.ui.Window;

class FlashWindow extends Window {
	
	this.FlashWindow = function(parent, x, y, width, height) {
		super(parent, x, y, width, height);
		this.params = {};
	}

	/**
	 * Sets source parameter for flash movie.
	 *
	 * @param src (string) movie source.
	 */
	this.setMovieSrc = function(src) {
		this.embed.setAttribute('src', src);
		this.setParam('movie', src);
	}
	
	/**
	 * Gets source parameter for flash movie.
	 *
	 * @return string
	 */
	this.getMovieSrc = function() {
		return this.getParam('movie');
	}
	
	/**
	 * Sets parameter for flash movie.
	 * Will NOT work after flash object is created and added to the document.
	 * 
	 * @param name (string) parameter name
	 * @param value (string) parameter value
	 */
	this.setParam = function(name, value) {
		if (!this.params[name]) {
			var param = document.createElement('param');
			param.setAttribute('name', name);
			this.object.appendChild(param);
			this.params[name] = param;
		}
		this.params[name].setAttribute('value', value);
		this.embed.setAttribute(name, value);
	}
	
	/**
	 * Gets flash movie parameter value.
	 * 
	 * @param name (string) parameter name.
	 * @return string
	 */
	this.getParam = function(name) {
		return (this.params[name]) ? this.params[name].getAttribute('value') : undefined;
	}
	
	this.close = function() {
		for (var name in this.params) {
			this.object.removeChild(this.params[name]);
			delete this.params[name];
		}
		// FIX: see above 
		try {
			this.object.removeChild(this.embed);
		} catch (ex) {
			this.getElement().removeChild(this.embed);
		}
		this.getElement().removeChild(this.object);
		delete this.object;
		delete this.embed;
		super.close();
	}
	
	/**
	 * @protected
	 * Creates root element for flash object.
	 *
	 * @return HTMLObjectElement
	 */
	this.createElement = function() {
		var root = super.createElement();
		
		this.object = document.createElement('object');
		this.object.setAttribute('type', 'application/x-shockwave-flash');
		this.object.setAttribute('width', '100%');
		this.object.setAttribute('height', '100%');
		root.appendChild(this.object);

		this.embed = document.createElement('embed');
		this.embed.setAttribute('type', 'application/x-shockwave-flash');
		this.embed.setAttribute('width', '100%');
		this.embed.setAttribute('height', '100%');
		
		try {
			// FIX: Looks like it's a bug in IE
			// when trying to append child embed to object element
			this.object.appendChild(this.embed);
		} catch (ex) {
			root.appendChild(this.embed);
			this.object.style.display = 'none';
		}
		
		return root;
	}
}